package com.gongJiao.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.gongJiao.Vo.*;
import com.gongJiao.common.Result;
import com.gongJiao.entity.*;
import com.gongJiao.service.RouteService;
import com.gongJiao.service.RouteSiteService;
import com.gongJiao.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  路线前端控制器
 * </p>
 * @author ${author}
 * @since 2021-12-25
 */
@Controller
@RequestMapping("/route")
public class RouteController {
    @Autowired
    RouteService routeService;
    @Autowired
    RouteSiteService routeSiteService;
    @Autowired
    VehicleService vehicleService;

    /**
     * 获取路线信息集合
     * @param session
     * @return
     */
    @GetMapping("getRouteList")
    public Result getRouteList(HttpSession session){
        List<Route> routeList=routeService.getRouteList();
        session.setAttribute("routeList",routeList);
        return Result.success("成功");
    }

    /**
     * 后台分页查询路线信息列表
     * @return
     */
    @GetMapping("getRoutePageList")
    @ResponseBody
    public PageResult<RouteVo> getVehiclePageList(PageCondition pageCondition){
        PageResult<RouteVo> pageResult=new PageResult<RouteVo>();
            //根据名字查询
            //查询所以数据
            List<RouteVo> list=routeService.getRouteListByName(pageCondition);
            //查询总记录数
            Integer count=routeService.getCountByName(pageCondition);
            return pageResult.success(list,count);
    }

    /**
     * 判断路线名是否可用
     * @param name
     * @return
     */
    @GetMapping("/judgeAvailable")
    @ResponseBody
    public Result judgeAvailable(String name){
        if ("".equals(name)){
            return Result.success("");
        }
        Route route=routeService.getVehicleByName(name);
        if (route == null) {
            return Result.success("分类名可用");
        }
        return Result.fail("分类名已存在不可用");
    }

    /**
     * 添加路线
     * @param routeRequest
     * @return
     */
    @PostMapping("addRoute")
    @ResponseBody
    public Result addRoute(RouteRequest routeRequest){
        Route route = new Route();
        route.setName(routeRequest.getName());
        route.setEnd(routeRequest.getEnd());
        route.setStartingPoint(routeRequest.getStartingPoint());
        long id=routeService.addRoute(route);
        if (id == 0) {
            return Result.fail("新增失败");
        }else {
            String xmselectName = routeRequest.getXmselectName();
            String[] split = xmselectName.split(",");
            int[] array = Arrays.stream(split).mapToInt(Integer::parseInt).toArray();
            for (int i = 0; i < array.length; i++){
                RouteSite routeSite = new RouteSite();
                routeSite.setRouteId(id);
                routeSite.setNumber(i);
                routeSite.setSiteId((long) array[i]);
                routeSiteService.save(routeSite);
            }
            return Result.success("新增成功");
        }
    }

    /**
     * 根据id获取路线信息
     * @param id
     * @return
     */
    @GetMapping("/getRoute/{id}")
    public String getRouteById(@PathVariable Long id, Model model){
        //获取公交信息
        Route route=routeService.getRouteById(id);
        model.addAttribute("route",route);
        return "admin/routeEdit";
    }

    /**
     * 修改信息
     * @param routeRequest
     * @return
     */
    @PostMapping("updateRoute")
    @ResponseBody
    public Result updateRoute(RouteRequest routeRequest){
        Route route = new Route();
        route.setId(routeRequest.getId());
        route.setName(routeRequest.getName());
        route.setStartingPoint(routeRequest.getStartingPoint());
        route.setEnd(routeRequest.getEnd());
        //修改路线表
        int row=routeService.updateRouteById(route);
        if (row > 0) {
            //修改关联表
            routeSiteService.updateByRouteId(routeRequest.getId(),routeRequest.getXmselectName());
            return Result.success("路线信息修改成功");
        }
        return Result.fail("路线信息修改失败！");
    }

    /**
     * 删除路线信息
     * @param ids
     * @return
     */
    @PostMapping("/{ids}")
    @ResponseBody
    public Result deleteRouteByIds(@PathVariable("ids") Long ids){
        //判断路线是否被使用
        String vehicleNames=vehicleService.getVehicleByNames(ids);
        System.out.println(vehicleNames);
        if (vehicleNames == null) {
            routeService.deleteRouteByIds(ids);
            return Result.success("路线删除成功");
        }else {
            return Result.fail("该路线已经被公交"+vehicleNames+"所使用，请先修改相关公交信息在进行删除");
        }

    }
}

